package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.Addchannelform;
import com.woniuxy.controller.form.Delechannelform;
import com.woniuxy.controller.form.FindAllChannelform;
import com.woniuxy.controller.form.FindChannelBylikeform;
import com.woniuxy.dao.MemberChannelRelDao;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.DelechannelIsNullExption;
import com.woniuxy.exception.ShopCodeCopeExption;
import com.woniuxy.model.Channel;
import com.woniuxy.dao.ChannelDao;
import com.woniuxy.model.MemberChannelRel;
import com.woniuxy.model.PawnShop;
import com.woniuxy.service.ChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-25
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelDao, Channel> implements ChannelService {
    @Resource
    private ChannelDao channelDao;
    @Resource
    private MemberChannelRelDao memberChannelRelDao;

    //分页全查询
    @Override
    public Result findchannelpage(FindAllChannelform findAllChannelform) {
        Page<Channel> ChannelPahe = baseMapper.selectPage(findAllChannelform, null);

        return Result.success(200,"查询成功",ChannelPahe);
    }
    //分页模糊查询
    @Override
    public Result findchannelpagelike(FindChannelBylikeform findAllChannelform) {
        Page<Channel> ChannelPahe= baseMapper.selectPage(findAllChannelform,new QueryWrapper<Channel>().like("channel_name",findAllChannelform.getLikeName())
        .or().like("channel_phone",findAllChannelform.getLikeName()));
        return Result.success(200,"查询成功",ChannelPahe);
    }
    //对多个多个或者单个渠道商进行删除
    @Override
    public Result delechannel(Delechannelform delechannelform) throws DdException {
        List<Channel> listchannel = delechannelform.getListchannel();
        if (ObjectUtils.isEmpty(listchannel)) throw new DelechannelIsNullExption(5004,"请先勾选要删除的渠道商");
        for (Channel listshop : listchannel) {
            baseMapper.delete(new QueryWrapper<Channel>().eq("channel_id",listshop.getChannelId()));
            memberChannelRelDao.delete(new QueryWrapper<MemberChannelRel>().eq("channel_id",listshop.getChannelId()));

        }
        return Result.success(200,"删除成功",null);
    }
    //新增一个渠道商
    @Override
    public Result addchannel(Addchannelform addchannelform) throws DdException {
        if (!ObjectUtils.isEmpty(channelDao.selectOne(new QueryWrapper<Channel>()
                .eq("channel_name",addchannelform.getChannelName()))))
            throw new ShopCodeCopeExption(50002,"该渠道名已存在重新输入");
        channelDao.insert(new Channel(null,addchannelform.getChannelName(),
                addchannelform.getChannelPhone(),
                addchannelform.getChannelStat(),
                null,null,null,addchannelform.getChannelIdNumber(),
                addchannelform.getChannelBank(),
                addchannelform.getChannelBankAccount()));

        return Result.success(200,"新增成功",null);
    }

}

